package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type YuyandayBody struct {
	Uid      int64   `json:"uid" gorm:"uid"`
	ID       int64   `json:"id" gorm:"id"`
	Page     int     `json:"page"`
	Pagesize int     `json:"pagesize"`
	Classify int64   `json:"classify"`
	Daibi    float64 `json:"daibi"`
	State    int64   `json:"state" gorm:"state"`
	Status   int64   `json:"status" gorm:"status"`
}

func Yuyanuserlist(ctx *gin.Context) {
	request := YuyandayBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	classify := request.Classify
	status := request.Status
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var yuyans []models.Yuyan
	res := data_source.Db.Where("uid = ?", uid).Debug()
	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	res.Model(&models.Yuyan{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&yuyans)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    yuyans,
		"msg":     "ok",
	})
	return
}

func Yuyanadd(ctx *gin.Context) {
	request := YuyandayBody{}
	ctx.ShouldBind(&request)
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	uid := request.Uid
	classify := request.Classify
	daibi := request.Daibi
	state := request.State
	if daibi <= 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "矿石输入错误",
		})
		return
	}
	var yuyans models.Yuyan
	data_source.Db.Where("daytime = ?", zeroToday).Where("uid = ?", uid).Where("classify = ?", classify).First(&yuyans)
	if yuyans.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "您已经参与过了",
		})
		return
	}

	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	if user.Daibi < daibi {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "矿石不足请充值",
		})
		return
	}
	var yyjg models.Yuyanjieguo
	data_source.Db.Where("daytime = ?", zeroToday).Where("classify = ?", classify).First(&yyjg)

	if state == 1 {
		yyjgupdate := models.Yuyanjieguo{
			Cannum:   yyjg.Cannum + 1,
			Candaibi: yyjg.Candaibi + daibi,
		}
		data_source.Db.Where("id = ?", yyjg.ID).Updates(&yyjgupdate)
	} else if state == 2 {
		yyjgupdate := models.Yuyanjieguo{
			Nocannum:   yyjg.Nocannum + 1,
			Nocandaibi: yyjg.Nocandaibi + daibi,
		}
		data_source.Db.Where("id = ?", yyjg.ID).Updates(&yyjgupdate)
	}

	yyadd := models.Yuyan{
		Uid:           uid,
		Addtime:       time.Now().Unix(),
		Daytime:       zeroToday,
		Classify:      classify,
		Daibi:         daibi,
		State:         state,
		Yuyanjieguoid: yyjg.ID,
		Status:        1,
		Obtaindaibi:   0,
	}
	data_source.Db.Create(&yyadd)

	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("daibi", user.Daibi-daibi)

	userdaibi := models.Userdaibi{
		Uid:     uid,
		Status:  2,
		Addtime: time.Now().Unix(),
		Daibi:   daibi,
		Title:   "参与每日预言",
		State:   9,
		Sydaibi: user.Daibi - daibi,
	}
	data_source.Db.Create(&userdaibi)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Yuyanlist(ctx *gin.Context) {
	request := YuyandayBody{}
	ctx.ShouldBind(&request)
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	classify := request.Classify
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var yuyanjieguo []models.Yuyanjieguo
	res := data_source.Db.Where("daytime < ?", zeroToday)
	if classify > 0 {
		res = res.Where("classify = ?", classify)
	}
	res.Model(&models.Yuyanjieguo{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&yuyanjieguo)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    yuyanjieguo,
		"msg":     "ok",
	})
	return
}

func Yuyanday(ctx *gin.Context) {
	request := YuyandayBody{}
	ctx.ShouldBind(&request)
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	uid := request.Uid
	var yuyanjieguo []models.Yuyanjieguo
	data_source.Db.Where("daytime = ?", zeroToday).Find(&yuyanjieguo)
	if len(yuyanjieguo) == 0 {
		var yuyanjieguos []models.Yuyanjieguo
		yyjgadd1 := models.Yuyanjieguo{
			Daytime:  zeroToday,
			Classify: 1,
			Jieguo:   0,
			Daibi:    0,
			Status:   1,
		}
		yyjgadd2 := models.Yuyanjieguo{
			Daytime:  zeroToday,
			Classify: 2,
			Jieguo:   0,
			Daibi:    0,
			Status:   1,
		}
		yuyanjieguos = append(yuyanjieguos, yyjgadd1)
		yuyanjieguos = append(yuyanjieguos, yyjgadd2)
		data_source.Db.Create(&yuyanjieguos)
		var yuyanjieguo1 []models.Yuyanjieguo
		data_source.Db.Where("daytime = ?", zeroToday).Find(&yuyanjieguo1)
		yuyanjieguo = yuyanjieguo1
	}
	var yuyans []models.Yuyan
	data_source.Db.Where("daytime = ?", zeroToday).Where("uid = ?", uid).Find(&yuyans)
	for k, y := range yuyanjieguo {
		yuyanjieguo[k].Status = 1
		for _, yy := range yuyans {
			if y.ID == yy.Yuyanjieguoid && y.Classify == yy.Classify {
				yuyanjieguo[k].Status = 2
			}
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    yuyanjieguo,
		"msg":     "ok",
	})
	return
}
func Yuyanconfig(ctx *gin.Context) {

	var users []models.Config
	data_source.Db.Where("id = 1 or id = 2").Find(&users)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    users,
		"msg":     "ok",
	})
	return
}

func Yuyandayjieguo(ctx *gin.Context) {
	request := YuyandayBody{}
	ctx.ShouldBind(&request)
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()

	var jiazhiprice float64
	var num float64
	var usergoods []models.Usergood
	data_source.Db.Where("state = 2 or state = 3").Where("addtime = ?", zeroToday).Find(&usergoods)

	for _, u := range usergoods {
		jiazhiprice = jiazhiprice + u.Goodprice
	}

	var orderniudannum int64
	data_source.Db.Model(&models.Orderniudangood{}).Where("addtime = ?", zeroToday).Where("dengji = 1 or dengji = 2").Count(&orderniudannum)
	var orderfanshangnum int64
	data_source.Db.Model(&models.Orderfanshanggood{}).Where("addtime = ?", zeroToday).Where("dengji = 1 or dengji = 2").Count(&orderfanshangnum)
	num = float64(orderniudannum) + float64(orderfanshangnum)

	var config []models.Config
	data_source.Db.Find(&config)

	for _, c := range config {
		if c.ID == 1 {
			if jiazhiprice >= utils.StrToFloat64(c.Value) {
				yyjgadd1 := models.Yuyanjieguo{
					Jieguo:         1,
					Chushangjieguo: jiazhiprice,
				}
				data_source.Db.Where("classify = 1").Where("daytime = ?", zeroToday).Updates(&yyjgadd1)
			} else {
				yyjgadd1 := models.Yuyanjieguo{
					Jieguo:         2,
					Chushangjieguo: jiazhiprice,
				}
				data_source.Db.Where("classify = 1").Where("daytime = ?", zeroToday).Updates(&yyjgadd1)
			}
		} else {
			if num >= utils.StrToFloat64(c.Value) {
				yyjgadd2 := models.Yuyanjieguo{
					Jieguo:         1,
					Chushangjieguo: num,
				}
				data_source.Db.Where("classify = 2").Where("daytime = ?", zeroToday).Updates(&yyjgadd2)
			} else {
				yyjgadd2 := models.Yuyanjieguo{
					Jieguo:         2,
					Chushangjieguo: num,
				}
				data_source.Db.Where("classify = 2").Where("daytime = ?", zeroToday).Updates(&yyjgadd2)
			}
		}

	}
	var yuyanjieguo []models.Yuyanjieguo
	data_source.Db.Where("daytime = ?", zeroToday).Find(&yuyanjieguo)

	for _, y := range yuyanjieguo {
		var yuyans []models.Yuyan
		data_source.Db.Where("yuyanjieguoid = ?", y.ID).Find(&yuyans)
		var dadaoprice float64
		var weidadaoprice float64
		for _, yy := range yuyans {
			if yy.State == 1 {
				dadaoprice = dadaoprice + yy.Daibi
			}
			if yy.State == 2 {
				weidadaoprice = weidadaoprice + yy.Daibi
			}
		}
		if y.Jieguo == 1 {
			var userdaibis []models.Userdaibi
			for _, yy1 := range yuyans {
				if yy1.State == 1 {
					hddaibi := int64(yy1.Daibi/dadaoprice*weidadaoprice + yy1.Daibi*100)
					hddaibi1 := float64(hddaibi / 100)
					var user models.User
					data_source.Db.Where("id = ?", yy1.Uid).First(&user)
					data_source.Db.Model(&models.User{}).Where("id = ?", yy1.Uid).Update("daibi", user.Daibi+hddaibi1)
					userdaibi := models.Userdaibi{
						Uid:     yy1.Uid,
						Status:  2,
						Addtime: time.Now().Unix(),
						Daibi:   hddaibi1,
						Title:   "参与每日预言",
						State:   10,
						Sydaibi: user.Daibi + hddaibi1,
					}
					userdaibis = append(userdaibis, userdaibi)
					data_source.Db.Model(&models.Yuyan{}).Where("id = ?", yy1.ID).Update("status", 2)
				} else {
					data_source.Db.Model(&models.Yuyan{}).Where("id = ?", yy1.ID).Update("status", 3)
				}
			}
			data_source.Db.Create(&userdaibis)
		} else if y.Jieguo == 2 {

			var userdaibis []models.Userdaibi
			for _, yy1 := range yuyans {
				if yy1.State == 2 {
					hddaibi := int64(yy1.Daibi/weidadaoprice*dadaoprice + yy1.Daibi*100)
					hddaibi1 := float64(hddaibi / 100)
					var user models.User
					data_source.Db.Where("id = ?", yy1.Uid).First(&user)
					data_source.Db.Model(&models.User{}).Where("id = ?", yy1.Uid).Update("daibi", user.Daibi+hddaibi1)
					userdaibi := models.Userdaibi{
						Uid:     yy1.Uid,
						Status:  2,
						Addtime: time.Now().Unix(),
						Daibi:   hddaibi1,
						Title:   "参与每日预言",
						State:   10,
						Sydaibi: user.Daibi + hddaibi1,
					}
					userdaibis = append(userdaibis, userdaibi)
					data_source.Db.Model(&models.Yuyan{}).Where("id = ?", yy1.ID).Update("status", 2)
				} else {
					data_source.Db.Model(&models.Yuyan{}).Where("id = ?", yy1.ID).Update("status", 3)
				}
			}
			data_source.Db.Create(&userdaibis)
		}
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
